local http = require("http")
local ltn12 = require("ltn12")
local htmlparser = require("htmlparser")

-- 设置请求头
local headers = {
    ["User-Agent"] =
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
}

-- 打开文件，以写入模式创建或覆盖
local file = io.open("movie.txt", "w")

for i = 1, 10 do
    -- 因为一页只有25个电影简介，所以需要10页
    local href = 'https://movie.douban.com/top250?start=' .. tostring((i - 1) * 25) .. '&filter='

    -- 发送HTTP请求获取页面内容
    local response_body, code, response_headers = http.request("GET", href, { headers = headers })

    if code == 200 then
        local html = response_body

        -- 使用HTML解析库解析网页代码
        local dom = htmlparser.parse(html)

        -- 需要爬取序号、电影名、评分、推荐语、链接
        local movies = dom("ol.grid_view li")
        for _, movie in ipairs(movies) do
            local movie_num = movie("div.pic em"):text()
            local movie_name = movie("div.info span.title"):text()
            local movie_star = movie("div.star span.rating_num"):text()
            local movie_recommend = movie("p.quote"):text()
            local movie_href = movie("div.pic a"):attr("href")

            if movie_name ~= "" and movie_href ~= "" and movie_num ~= "" and movie_star ~= "" and movie_recommend ~= "" then
                print('排行第' .. movie_num .. '的电影:' .. '\n')
                print('电影名：' .. movie_name .. '\t' .. '评分：' .. movie_star)
                print('推荐语：' .. movie_recommend .. '\n')
                print('链接：' .. movie_href)
                print('\n' .. '******************************' .. '\n')

                -- 将结果写入文件
                file:write('排行第' .. movie_num .. '的电影:' .. '\n')
                file:write('电影名：' .. movie_name .. '\t' .. '评分：' .. movie_star .. '\n')
                file:write('推荐语：' .. movie_recommend .. '\n')
                file:write('链接：' .. movie_href .. '\n')
                file:write('\n' .. '******************************' .. '\n')
            end
        end
    else
        print("Failed to fetch page:", code)
    end
end

-- 关闭文件
file:close()

-- luarocks install luasocket
-- luarocks install htmlparser
